<?php
/**
 * Created by PhpStorm.
 * User: yuxingkui
 * Date: 2017/8/11
 * Time: 下午1:13
 */
namespace App\Api\Controllers\V1;

use App\Api\Controllers\Validators\ApplyValidator;
use App\Api\Helper\ReturnMessage;
use App\Models\api\Apply;
use App\Http\Validators\CompanyValidator;
use App\Services\ApplyService;
use App\Services\CompanyService;
use App\Services\LiveService;
use Illuminate\Http\Request;

class ApplyController extends BaseController
{

    /**
     * 企业申请
     *
     * @param Request $request
     * @return mixed
     */
    public function apply(Request $request)
    {
        $input = ApplyValidator::apply($request);

        $res = Apply::create($input);

        if ($res)
            return ReturnMessage::success();

        return ReturnMessage::success('fail',1001);

    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author Liying
     * @name 查询企业信息
     */
    public function applyShow(Request $request)
    {
        $res = CompanyService::getAllCompanyLists($request);
        if ($res)
            return ReturnMessage::successData($res);
        return ReturnMessage::success();
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author Liying
     * @name 查询企业详情信息
     */
    public function applyInfo(Request $request)
    {
        $company_id = $request->get('company_id')?:0;
        $res = LiveService::liveCompanyInfo($company_id);
        if ($res)
            return ReturnMessage::successData($res);

        return ReturnMessage::success('fail',1001);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author Liying
     * @name 修改企业类型
     */
    public function applyEdit(Request $request)
    {
        $company_id = $request->get('company_id')?:0;
        $res = CompanyValidator::save($request,$company_id);
        $res = CompanyService::applyEdit($res);

        if ($res)
            return ReturnMessage::success('更新成功');

        return ReturnMessage::success('更新失败',1002);

    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author Liying
     * @name 修改有效期限
     */
    public function applyTerm(Request $request)
    {
        $res = CompanyService::companyUseTerm($request);
        if ($res)
            return ReturnMessage::success('操作成功！','1000',$res);

        return ReturnMessage::success('操作失败！',1001);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @throws \Exception
     * @author Liying
     * @name 申请企业测试号
     */
    public function applyReg(Request $request){
        $res = ApplyValidator::applyReg($request);
        $errmsg="操作失败";
        $res = ApplyService::applyCreate($res,$errmsg);
        if ($res)
            return ReturnMessage::success('申请成功！','1000');

        return ReturnMessage::success($errmsg,1001);
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @name 申请学习卡企业
     * @user IHammer
     */
    public function applyCard(Request $request){
        $res = ApplyValidator::applyCardCreate($request);
        $errmsg="操作失败";
        $res = ApplyService::applyCardCreate($res,$errmsg);
        if ($res)
            return ReturnMessage::success('申请成功！','1000');

        return ReturnMessage::success($errmsg,1001);
    }

    /**
     * 关于我们
     *
     * */
    public function about(  )
    {
        $data['url'] = env('QKW_WEB_URL').'about/html/index.html';
        return ReturnMessage::successData($data);
    }

    /**
     * 页面限制
     * */
    public function restriction(  )
    {
        if (time() < strtotime('2017-9-10')){
            $data['status'] = "false";
        }else{
            $data['status'] = "true";
        }
        return ReturnMessage::successData($data);
    }

    /**
     * 获取申请账户列表
     */
    public function applyList(Request $request){
        $page['page']=$request->get('page')?:1;
        $page['pageSize']=$request->get('pageSize')?:10;

    }


}